package com.useeinfo.oa.modules.security.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 系统权限表
 */
@Entity
@Table(name = "system_authority")
public
class SystemAuthority extends SuperClassEntity<SystemAuthority> {

    private String authorityName;

    private String authorityDescription;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "system_authority_2_resource", joinColumns = @JoinColumn(name = "authority_id"), inverseJoinColumns = @JoinColumn(name = "resource_id"))
    private Set<SystemResource> resourceSet=new HashSet<>();

    @ManyToMany(mappedBy = "authoritySet")
    private Set<SystemRole> roleSet=new HashSet<>();

    private boolean systemManager;

    private boolean enabled;
    /**
     * 所属权限模块
     */
    @ManyToOne
    @JoinColumn(name = "system_module_id")
    private SystemModule systemModule;


    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        SystemModule systemModule = CommonSugar.getTypedDefault(getSystemModule(), new SystemModule());
        jsonObject.put("authorityId", CommonSugar.getTypedDefault(getId(), "0L"));
        jsonObject.put("createTime", CommonSugar.getTypedDefault(DateTimeUtils.formatDateToStringWithTime(getCreateDate()), ""));
        jsonObject.put("authorityName", CommonSugar.getTypedDefault(getAuthorityName(), ""));
        jsonObject.put("authorityDescription", CommonSugar.getTypedDefault(getAuthorityDescription(), ""));
        jsonObject.put("moduleName", CommonSugar.getTypedDefault(systemModule.getModuleName(), ""));
        jsonObject.put("systemManager", isSystemManager());
        jsonObject.put("enabled", isEnabled());
        return jsonObject;
    }

    public SystemAuthority() {
    }

    public SystemAuthority(String authorityName,
                           String authorityDescription,
                           Set<SystemRole> roleSet,
                           Set<SystemResource> resourceSet,
                           boolean systemManager,
                           boolean enabled) {

        this.authorityName = authorityName;
        this.authorityDescription = authorityDescription;
        this.resourceSet = resourceSet;
        this.systemManager = systemManager;
        this.enabled = enabled;
    }

    public String getAuthorityName() {
        return authorityName;
    }

    public void setAuthorityName(String authorityName) {
        this.authorityName = authorityName;
    }

    public String getAuthorityDescription() {
        return authorityDescription;
    }

    public void setAuthorityDescription(String authorityDescription) {
        this.authorityDescription = authorityDescription;
    }

    public Set<SystemResource> getResourceSet() {
        return resourceSet;
    }

    public void setResourceSet(Set<SystemResource> resourceSet) {
        this.resourceSet = resourceSet;
    }

    public boolean isSystemManager() {
        return systemManager;
    }

    public void setSystemManager(boolean systemManager) {
        this.systemManager = systemManager;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void addResource(SystemResource systemResource) {
        this.resourceSet.add(systemResource);
    }

    public void removeResource(SystemResource systemResource) {
        this.resourceSet.remove(systemResource);
    }

    public Set<SystemRole> getRoleSet() {
        return roleSet;
    }

    public void setRoleSet(Set<SystemRole> roleSet) {
        this.roleSet = roleSet;
    }

    public SystemModule getSystemModule() {
        return systemModule;
    }

    public void setSystemModule(SystemModule systemModule) {
        this.systemModule = systemModule;
    }
}
